package com.moon.interface_api.utils;


import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;

/**
 *Created by chengliang on 2018/11/28.
 */
public class IDCardUtil {

    private static final String gerderMale = "男";
    private static final String gerderFeMale = "女";
    private static final String gentlemen = "先生";
    private static final String ladie = "女士";


    /**
     * 根据身份证的号码算出当前身份证持有者的性别
     * (默认使用先生、女士)
     *
     * @return
     * @throws Exception
     */
    public static String getGenderByIDcardNo(String IDcardNo) {
        return getGenderByIDcardNo(IDcardNo, false);
    }

    /**
     * 根据身份证的号码算出当前身份证持有者的性别
     *
     * @return
     * @throws Exception
     */
    public static String getGenderByIDcardNo(String IDcardNo, boolean isSex) {
        String gerder = "";
        if (isSex) {
            if (IDcardNo.length() == 18) {
                if (Integer.parseInt(IDcardNo.substring(16).substring(0, 1)) % 2 == 0) {// 判断性别
                    gerder = gerderFeMale;
                } else {
                    gerder = gerderMale;
                }
            } else if (IDcardNo.length() == 15) {
                if (Integer.parseInt(IDcardNo.substring(14, 15)) % 2 == 0) {
                    gerder = gerderFeMale;
                } else {
                    gerder = gerderMale;
                }
            }
        } else {
            if (IDcardNo.length() == 18) {
                if (Integer.parseInt(IDcardNo.substring(16).substring(0, 1)) % 2 == 0) {// 判断性别
                    gerder = ladie;
                } else {
                    gerder = gentlemen;
                }
            } else if (IDcardNo.length() == 15) {
                if (Integer.parseInt(IDcardNo.substring(14, 15)) % 2 == 0) {
                    gerder = ladie;
                } else {
                    gerder = gentlemen;
                }
            }
        }
        return gerder;
    }

    /**
     * 根据身份证号获取实际年龄（周岁）
     * @param IDcardNo
     * @return
     */
    public static int getAgeByIDcardNo(String IDcardNo) {
        int leh = IDcardNo.length();
        String dates = "";

        if(leh == 18){
            dates = IDcardNo.substring(6, 14);
        }else if(leh == 15){
            dates = "19"+IDcardNo.substring(6, 12);
        }else {
            return -1;
        }
        Date birthDay = null;
        try {
            birthDay = DateUtils.parseDate(dates, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }

        LocalDate today = LocalDate.now();
        int yearNow = today.getYear();
        int monthNow = today.getMonthValue();
        int dayOfMonthNow = today.getDayOfMonth();

        Calendar cal = Calendar.getInstance();
        cal.setTime(birthDay);
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH)+1;
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;
            }else{
                age--;
            }
        }

        return age;
    }


    public static void main(String[] args) {
//        System.out.println(getAgeByIDcardNo("130503670401001")); //320621198804303
//        System.out.println(getGenderByIDcardNo("110105710923582"));
//        System.out.println(getGenderByIDcardNo("431103199405160"));
    }


}
